//
//  APPLENewFeatureViewController.swift
//  123
//
//  Created by pangjiaxing on 15/12/31.
//  Copyright © 2015年 pangjiaxing. All rights reserved.
//

import UIKit

class APPLENewFeatureViewController: UIViewController,UIScrollViewDelegate {
    
    private lazy var scrollView:UIScrollView = {
        let scrollview=UIScrollView()
        scrollview.backgroundColor = UIColor.blueColor()
        scrollview.pagingEnabled=true
        scrollview.bounces=false
        scrollview.showsHorizontalScrollIndicator=false
        scrollview.delegate=self
        return scrollview
    }()
    
    private lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor=UIColor.purpleColor()
        return pageControl
    }()
    
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("进入微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    
    private lazy var sharedButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)

        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        button.setTitle("分享到微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5)
        return button
    }()
    
    @objc private func sharedButtonClick(button:UIButton){
        button.selected = !button.selected
    }
    
    @objc private func enterButtonClick(){
        print("APPLENewFeatureViewController跳转页面")
                NSNotificationCenter.defaultCenter().postNotificationName("HMSwitchRootViewControllerNotification", object: nil)
    }
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        setUpUI()
    }
    
    private func setUpUI(){
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        
        scrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-100)
        }
        
        let count = 4
        for i in 0 ..< count {
            let imageview = UIImageView()
            let X = CGFloat(i) * mainScreenW
            let W = mainScreenW
            let H = mainScreenH
            imageview.frame=CGRect(x: X , y: 0, width: W, height: H)
//            imageview.image=UIImage(named: "new_feature_\(i + 1)")
            let image=UIImage(named: "new_feature_\(i + 1)")
            imageview.image = image
            
            if i == count-1 {
                setUpLastPage(imageview)
            }
            scrollView.addSubview(imageview)
            scrollView.contentSize = CGSizeMake(CGFloat(count)*mainScreenW, 0)
            pageControl.numberOfPages = count
        }
    }
    private func setUpLastPage(imageView:UIImageView){
        imageView.userInteractionEnabled=true
        imageView.addSubview(enterButton)
        imageView.addSubview(sharedButton)
        
        enterButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView)
            make.centerY.equalTo(imageView.snp_bottom).offset(-150)
        }
        sharedButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(enterButton)
            make.bottom.equalTo(enterButton.snp_top).offset(-20)
        }
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x/scrollView.frame.size.width + 0.5)
        
        pageControl.currentPage=page
    }
}
